package com.uenpay.im.ui.old.activity.red;

import android.annotation.SuppressLint;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.aswife.ui.RoundedImageView;
import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.listener.OnHttpRequestTextListener;
import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.net.HttpRequestAsyncTaskQueue;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.net.HttpTextAsyncTask;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.MyDialog;
import com.uenpay.im.weight.listview.RefreshListView;
import com.uenpay.util.app.base.BaseActivity;

import java.text.MessageFormat;
import java.util.ArrayList;

/**
 * 接受和发送红包记录
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class ReceviedOrSendRedActivity extends BaseActivity implements
        RefreshListView.IRefreshListViewListener {

    private LinearLayout year_layout;
    private TextView money_tv, red_count_tv, year_tv, type_tv;
    private RoundedImageView avatar_iv;
    private RefreshListView red_listview;
    private RedAdapter adapter;
    private int type = 1, page = 1;
    private String selectedYear = "";
    private ArrayList<String> years = new ArrayList<String>();
    private RelativeLayout red_type_send_layout, red_type_received_layout;
    private View red_type_received_view, red_type_send_view;
    private TextView red_type_received_tv, red_type_send_tv;
    private View loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_red_record_layout;
    }

    @Override
    public void initView(View view) {
        type = getIntent().getIntExtra("type", 1);
        initView();
        initListener();
        getRedYear();
        updateSelectedType(false);
    }

    protected void initView() {
        red_type_received_layout = (RelativeLayout) findViewById(R.id.red_type_received_layout);
        red_type_send_layout = (RelativeLayout) findViewById(R.id.red_type_send_layout);
        red_type_received_view = findViewById(R.id.red_type_received_view);
        red_type_send_view = findViewById(R.id.red_type_send_view);
        red_type_received_tv = (TextView) findViewById(R.id.red_type_received_tv);
        red_type_send_tv = (TextView) findViewById(R.id.red_type_send_tv);
        red_listview = (RefreshListView) findViewById(R.id.discover_listview);
        loading = findViewById(R.id.loading);
        View header = View.inflate(this, R.layout.red_list_header_layout, null);
        money_tv = (TextView) header.findViewById(R.id.money_tv);
        red_count_tv = (TextView) header.findViewById(R.id.red_count_tv);
        type_tv = (TextView) header.findViewById(R.id.red_type);
        year_tv = (TextView) header.findViewById(R.id.year_tv);
        avatar_iv = (RoundedImageView) header.findViewById(R.id.avatar_iv);
        year_layout = (LinearLayout) header.findViewById(R.id.year_layout);

        red_listview.addHeaderView(header);
        red_listview.setAdapter(new RedAdapter(this,
                new ArrayList<RedEntity>(), type));
        avatar_iv.SetUrl(APIConfiguration.getAvatarUrlBig(
                IMApplication.getCustId(), PacketType.PERSONAL));
    }

    protected void initListener() {
        red_listview.setPullLoadEnable(true);
        red_listview.setRefreshListViewListener(this);
        year_layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                updateYearDailog();
            }
        });
        red_type_send_layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                type = 2;
                updateSelectedType(true);
            }
        });
        red_type_received_layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                type = 1;
                updateSelectedType(true);
            }
        });
        findViewById(R.id.back_layout).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void updateSelectedType(boolean is_check) {
        if (type == 1) {
            // 接受到的红包
            red_type_received_view.setVisibility(View.VISIBLE);
            red_type_received_tv.setTextColor(getResources().getColor(
                    R.color.red_text_color));
            red_type_send_tv.setTextColor(getResources().getColor(
                    R.color.the_color_white));
            red_type_send_view.setVisibility(View.GONE);
        } else {
            // 发送的红包
            red_type_received_view.setVisibility(View.GONE);
            red_type_received_tv.setTextColor(getResources().getColor(
                    R.color.the_color_white));
            red_type_send_tv.setTextColor(getResources().getColor(
                    R.color.red_text_color));
            red_type_send_view.setVisibility(View.VISIBLE);
        }

        if (type == 1) {
            type_tv.setText(getResources().getString(
                    R.string.red_total_received));
        } else {
            type_tv.setText(getResources().getString(R.string.red_total_send));
        }

        if (is_check) {
            page = 1;
            getData();
        }
    }

    private void getRedYear() {
        GetRedOpenYearInvokItem item = new GetRedOpenYearInvokItem();
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.SERVER_ONLY),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                                IMApplication.showToast(reason);
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                GetRedOpenYearInvokItem.GetRedOpenYearInvokItemResult result = ((GetRedOpenYearInvokItem) item)
                                        .getOutput();
                                if (result.status == HttpInvokeResultCode.RESULT_OK) {
                                    years = result.years;
                                    year_tv.setText(MessageFormat.format(
                                            getResources().getString(
                                                    R.string.red_year),
                                            years.get(years.size() - 1)));
                                    selectedYear = years.get(years.size() - 1);
                                    getData();
                                }
                            }
                        });

    }

    private void getData() {
        GetSendOrReceivedRecordInvokItem item = new GetSendOrReceivedRecordInvokItem(
                type, selectedYear, page);
        HttpRequestAsyncTaskQueue
                .getInstance()
                .AddTask(
                        new HttpTextAsyncTask(item)
                                .SetRequestType(HttpRequestType.SERVER_ONLY),
                        new OnHttpRequestTextListener() {

                            @Override
                            public void OnFail(boolean isCache, String reason) {
                                onLoad();
                                IMApplication.showToast(reason);
                            }

                            @Override
                            public void OnProgress(long total_bytes,
                                                   long downloaded_bytes) {
                            }

                            @Override
                            public void OnSuccess(HttpInvokeItem item,
                                                  boolean isCache, String cacheFilePath) {
                                onLoad();
                                GetSendOrReceivedRecordInvokItem.GetSendOrReceivedRecordInvokItemResult result = ((GetSendOrReceivedRecordInvokItem) item)
                                        .getOutput();
                                if (result != null
                                        && result.status == HttpInvokeResultCode.RESULT_OK) {
                                    money_tv.setText(result.myPacketsTotalAmount);
                                    red_count_tv.setText(result.myPacketsNum
                                            + "");
                                    if (page == 1) {
                                        // if (adapter == null) {
                                        // adapter = new RedAdapter(
                                        // ReceviedOrSendRedActivity.this,
                                        // result.msg_list, type);
                                        // } else {
                                        // adapter.setData(result.msg_list);
                                        // }
                                        adapter = new RedAdapter(
                                                ReceviedOrSendRedActivity.this,
                                                result.arrayList, type);

                                        red_listview.setAdapter(adapter);
                                        red_listview.setPullLoadEnable(true);
                                    } else {
                                        if (result.arrayList != null
                                                && result.arrayList.size() > 0) {
                                            adapter.addData(result.arrayList);
                                        } else {
                                            red_listview
                                                    .setPullLoadEnable(false);
                                        }
                                    }
                                }
                            }
                        });

    }

    private void updateYearDailog() {
        final MyDialog.Builder dlg = new MyDialog.Builder(this);
        final ListView menus = new ListView(this);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        menus.setDivider(new ColorDrawable(this.getResources().getColor(
                R.color.layout_division_view)));
        menus.setDividerHeight(1);
        menus.setLayoutParams(params);
        menus.setTag(years);
        menus.setAdapter(new ArrayAdapter<String>(this,
                R.layout.custom_contenxt_menu_item, years));
        dlg.setListView(menus);
        dlg.setMessage("");
        dlg.create().show();

        menus.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int menuIndex, long id) {
                year_tv.setText(MessageFormat.format(
                        getResources().getString(R.string.red_year),
                        years.get(menuIndex)));
                page = 1;
                selectedYear = years.get(menuIndex);
                getData();
                dlg.getDialog().dismiss();

            }
        });
        dlg.getDialog().setCancelable(true);
    }

    private void onLoad() {
        red_listview.stopRefresh();
        red_listview.stopLoadMore();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }



}
